/*
 * ================================================================================变量定义区
 */
var showCount =10; //每页显示条数
var currentPage = 1;//当前页
var listData = "";//存储列表数据
/*
 * ================================================================================Page initialization function
 */
$(function()
{
	//初始化列表，取第一页数据
	getResourceList(currentPage);
});
/*
 * ================================================================================Functions defined area
 * 
 */

/**
 * 获取列表数据
 * @param pageIndex 当前页
 */
function getResourceList(pageIndex)
{
	$('#tBodyList').html("");
	$.ajax(
	{
		type: 'POST',
        url: BASE_PATH +'/resourceManagement/queryUrlsList.do',
        dataType:"json",
        data:
        {
        	"showCount":showCount,
        	"currentPage":pageIndex,
        },
        cache:false,
        success:function(data,textStatus)
    	{
        	getPageList(pageIndex, data);
        },
		error:function(e)
		{
			openAlert("加载数据错误！"); return;
		}
	});
} 

/**
 * 获取分页数据
 * @param pageIndex
 */
function getPageList(pageIndex, list){
	if(list){
		listData = list;
		//设置分页列表
		setPageList(pageIndex);
		//显示分页条
		var totalResult = list.length;
		if(totalResult == 0)
		{
			$('#tBodyList').html('<tr><td style="text-align:center;">No items.</td></tr>');
			$("#pagination").hide();
		} 
		else 
		{	
			$("#pagination").show();
			$("#pagination").pagination(totalResult, 
			{
				num_edge_entries: 1, 
				num_display_entries: 3, 
				current_page: pageIndex,
				callback: setPageList,
				items_per_page: showCount 
			});
		};
	}
}
/**
 * 设置分页数据
 * @param pageIndex
 */
function setPageList(pageIndex){
	//数组截取开始值
	var startNum = (pageIndex-1) * showCount;
	//数组截取结束值
	var endNum = startNum + showCount;
	//如果是最后一页
	if(endNum > listData.length){
		endNum = startNum + listData.length % showCount;
	}
	var resultList = listData.slice(startNum, endNum);
	var html = "";
	$.each(resultList,function(i,n)
	{
		html += '<tr onclick="chooseRow(this,false);"  id="tr_index_'
				+i+'">';
		
		if(n.protectedRes == 1)
		{
    		html += '<td style="text-align:left;width: 98.5%;" title=' +n.resUrl +'><input id="' +n.id 
    		        +'" type="hidden" value="' +n.id+'"/><font color="red">' +n.resUrl
    				+'</font></td>';
		}
		else
		{
			html += '<td style="text-align:left;width: 98.5%;" title=' +n.resUrl +'><input id="' +n.id 
	        +'" type="hidden" value="' +n.id+'"/>' +n.resUrl
			+'</td>';
		}
		
		html += '<td style="width: 1px;" >'+''+'</td></tr>';
	});  
	$('#tBodyList').html(html);
}